﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Google.API.Translate;
using System.Globalization;
using Sherwood.Localization;

namespace Sherwood.Localization.Google
{
    public class GoogleTranslationProvider : ITranslationProvider
    {
        public class TranslationWorkerState
        {
            public object CallbackState { get; set; }
            public TranslationCallback Callback { get; set; }
            public string Key { get; set; }
            public string DefaultValue { get; set; }
        }

        public GoogleTranslationProvider(string cultureName)
        {
            _translateClient = new TranslateClient("http://sherwood.codeplex.com/");
            SetCulture(cultureName);
        }
        #region ITranslationProvider Members

        TranslateClient _translateClient;
        string _fromLanguage = "en";
        string _toLanguage = "no";


        public void SetCulture(string cultureName)
        {
            CultureInfo culture = null;
            try
            {
                culture = new CultureInfo(cultureName);
            }
            catch {
                return;
            }
            while (!culture.IsNeutralCulture && culture.Parent != null)
            {
                culture = culture.Parent;
            }
            if (culture != null)
            {
                _toLanguage = culture.Name.Split('-')[0].Trim();
            }
        }

        /// <summary>
        /// Asynchronously translation corrosponding to the provided key in the active translation culture.
        /// Translation culture can be changed using the SetCulture() method.
        /// </summary>
        /// <param name="key">Translation key</param>
        /// <param name="defaultValue">The value to be returned if no translation is found.</param>
        /// <param name="callback">The function to call once translation has been obtained</param>
        public void GetStringAsync(string key, string defaultValue, TranslationCallback callback, object state)
        {
            TranslationWorkerState translationState = new TranslationWorkerState();
            translationState.Callback = callback;
            translationState.CallbackState = state;
            translationState.DefaultValue = defaultValue;
            translationState.Key = key;
            _translateClient.BeginTranslate(key, _fromLanguage, _toLanguage, FinishTranslate, translationState);
        }

        protected void FinishTranslate(IAsyncResult ar)
        {
            string translation = _translateClient.EndTranslate(ar);
            TranslationWorkerState state = (TranslationWorkerState)ar.AsyncState;
            if (!string.IsNullOrEmpty(state.DefaultValue) && string.IsNullOrEmpty(translation))
            {
                translation = state.DefaultValue;
            }
            state.Callback(state.Key, translation, state.CallbackState);

        }

        public string GetString(string key)
        {
            string retVal = _translateClient.Translate(key, _fromLanguage, _toLanguage);
            return retVal != null ? retVal : "";
        }

        public string GetString(string key, string defaultValue)
        {
            string retVal = _translateClient.Translate(key, _fromLanguage, _toLanguage);
            return retVal != null ? retVal : defaultValue;
        }

        public string this[string key]
        {
            get
            {
                return GetString(key);
            }
        }

        #endregion


    }
}
